package service;

import mapper.BrandMapper;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import pagination.PageBean;
import pojo.Brand;
import utils.SqlSessionFactoryUtils;

import java.util.List;

public class BrandService {

    public PageBean<Brand> selectAllPage(int curPage, int pageSize){
//1.开启会话
        SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();
        SqlSession sqlSession = factory.openSession();
//2.获取Mapper
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
//3.执行查询(做2个查询)
//【计算begin的索引】
        int beginIndex = (curPage -1 ) * pageSize;
        List<Brand> brandList = brandMapper.selectAllPage(beginIndex ,pageSize);
        int totalCount = brandMapper.selectCount();
//4.将数据封装为分页格式
        PageBean<Brand> pageBean = new PageBean<>();
        pageBean.setData(brandList);
        pageBean.setCurrentPage(curPage);
        pageBean.setPageSize(pageSize);
        pageBean.setTotalCount(totalCount);
// 计算： 总页数
        int totalPage = (int) Math.ceil( totalCount*1.0/pageSize );
        pageBean.setTotalPage(totalPage);
//5.sqlSession关闭
        sqlSession.close();
//6.返回数据
        return pageBean;
    }


    public List<Brand> selectAll(){
        SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();
        SqlSession sqlSession = factory.openSession();
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        List<Brand> brands = mapper.selectAll();
        sqlSession.close();
        return brands;
    }
    public void add(Brand brand){
        SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();
        SqlSession sqlSession = factory.openSession();
//3. 获取BrandMapper
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
//4. 调用方法
        brandMapper.add(brand);
//提交事务
        sqlSession.commit();
//释放资源
        sqlSession.close();
    }

    public Brand selectById(int id){
        SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();
        SqlSession sqlSession = factory.openSession();
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        List<Brand> brands = mapper.selectAll();
        Brand brand = mapper.selectById(id);
        sqlSession.close();
        return brand;

    }

//    品牌修改
    public void update(Brand brand){

//2. 获取SqlSession
        SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();
        SqlSession sqlSession = factory.openSession();
//3. 获取BrandMapper
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
//4. 调用方法
        brandMapper.update(brand);
//提交事务
        sqlSession.commit();
//释放资源
        sqlSession.close();
}

    public void delete(int id) {


        SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();
        SqlSession sqlSession = factory.openSession();
//3. 获取BrandMapper
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);

        brandMapper.deleteById(id);
//提交事务
        sqlSession.commit();
//释放资源
        sqlSession.close();

    }

    }
